package com.dkcd.chenshiproject.tabHostActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.dkcd.chenshiproject.MainActivity;
import com.dkcd.chenshiproject.R;
import com.dkcd.chenshiproject.activity.IndexActivity;
import com.dkcd.chenshiproject.activity.MusicPlayerActivity;
import com.dkcd.chenshiproject.adapter.ReCommendMusicAdapter;
import com.dkcd.chenshiproject.entity.Song;
import com.dkcd.chenshiproject.util.ArtworkUtils;
import com.dkcd.chenshiproject.util.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendFragment extends Fragment{
    private List<Song> musicData = MainActivity.musicData;
    private GridView gridView;
    private  List<Map<String, Object>> list = IndexActivity.list;

    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("TAG", "onCreateView: fragment_recommend onCreate执行了");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.e("TAG", "onCreateView: fragment_recommend执行了");
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_recommend, container, false);
        init(inflate);
        initView();
        return inflate;
    }


    private void init(View inflate){
        gridView = (GridView) inflate.findViewById(R.id.re_commend_grid_view);
    }

    private void initView(){
        initGridViewList();
    }

    @Override
    public void onStart() {
        super.onStart();
        selectMusic();
    }

    private void initGridViewList(){
        ReCommendMusicAdapter reCommendMusicAdapter = new ReCommendMusicAdapter(getContext(), list);
        gridView.setAdapter(reCommendMusicAdapter);
    }

    private void selectMusic(){
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), MusicPlayerActivity.class);
                Bundle bundle = new Bundle();
                Log.e("RecommendMusicSelect", "onItemClick: 推荐页面播放音乐"+i);
                SPUtils.putValue(view.getContext(),"position",i+IndexActivity.mIndex);
                //传入路径
                bundle.putString("musicPath",musicData.get(i).path);
                bundle.putString("maker","RecommendFragment");
                //传入id
                bundle.putInt("musicId",i+IndexActivity.mIndex);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}